package edu.cdnu.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.cdnu.common.dto.CourseManageDto;
import edu.cdnu.common.result.ResultEntity;
import edu.cdnu.pojo.CourseClassPojo;
import edu.cdnu.pojo.CoursePojo;
import edu.cdnu.pojo.CourseStudentPojo;
import edu.cdnu.pojo.CourseTeacherPojo;
import edu.cdnu.pojo.StudentPojo;
import edu.cdnu.service.CourseService;
import edu.cdnu.service.StudentService;
import edu.cdnu.service.TeacherService;

/**
 * 关于学生操作的Controller层
 * @author ASUS AsingZ
 */
@RestController
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private StudentService studentService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private TeacherService teacherService;
	
	/**
	 * 新增或者修改学生
	 * @param studentPojo
	 * @return
	 */
	@PostMapping("/addOrUpdateStudent")
	public ResultEntity<?> addOrUpdateStudent(@RequestBody StudentPojo studentPojo){
		if(studentPojo.getId() == null) {
			// 没得id为新增
			studentService.addStudent(studentPojo);
		}else {
			// 有id为修改
			studentService.updateStudent(studentPojo);
		}
		
		return ResultEntity.createSuccessResult(null);
	}
	
	
	/**
	 * 通过学生id查询出所在的班级的课程
	 * @param studentId
	 * @return
	 */
	@GetMapping("/courseList/{studentId}")
	public ResultEntity<List<CourseManageDto>> selectCourseListByStudentId(@PathVariable Integer studentId){
		List<CourseManageDto> coursePojoList = new ArrayList<>();
		// 1.先查出学生--获取学生所在班级
		StudentPojo student=studentService.selectStudentById(studentId);
		// 2. 再查询出班级所参加课程
		List<Integer> courseIdList=courseService.selectCourseIdListByClassIdList(student.getClassId());
		// 3. 通过课程id查询出课程列表
		List<CoursePojo> courseList=courseService.selectCourseListByCourseIdList(courseIdList);
		
		if(courseList!=null) {
			courseList.forEach((item)->{
				if(item!=null) {
					CourseManageDto c=new CourseManageDto(item.getId(), item.getName(), item.getDescription(), item.getSyllabus());
					coursePojoList.add(c);
				}
				
			});
		}
		
		// 遍历课程把对应关系查出来
		coursePojoList.forEach((item)->{
			// 1.课程--教师
			List<CourseTeacherPojo> ctList=courseService.selectCourseTeacherRelationshipListByCourseId(item.getId());
			// 2.课程--班级
			List<CourseClassPojo> ccList=courseService.selectCourseClassRelationshipListByCourseId(item.getId());
			// 3.课程--学员
			List<CourseStudentPojo> csList=courseService.selectCourseStudentRelationshipListByCourseId(item.getId());
			// 关系的list  set进去
			item.setRelationshipList(ctList, csList, ccList);
			
			// 循环对应关系，把详细的对象也查询出来
			// 1.课程--教师
			// 保存了一众教师的id
			List<Integer> teacherIdList=ctList.stream().map(ctItem->{
				return ctItem.getTeacherId();
			}).collect(Collectors.toList());
			// 教师对象数据 set进去
			item.setTeacherList(teacherService.selectTeacherListByIdList(teacherIdList));
			
			// 2.课程--班级  班级那些存在数据字典表里面
			// 这个后面再由前端查询吧
			
			// 3.课程--学员
			// 保存了一众学员的id
			List<Integer> studentIdList=csList.stream().map(csItem->{
				return csItem.getStudentId();
			}).collect(Collectors.toList());
			// 学生对象数据 set进去
			item.setStudentList(studentService.selectStudentListByIdList(studentIdList));
		});
		return ResultEntity.createSuccessResult(coursePojoList);
	}
	
}
